package com.market.controller.admin;

import com.market.bean.MarketRole;

import com.market.bean.bo.admin.AdminPermissionPostBo;
import com.market.bean.bo.admin.AdminRoleCreateBo;

import com.market.bean.bo.AdminPermissionBo;

import com.market.bean.common.BasePageInfo;
import com.market.bean.common.BaseRespVo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.admin.AdminOptionsVo;
import com.market.bean.vo.admin.AdminPermissionVo;
import com.market.bean.vo.admin.AdminRoleCreateVo;
import com.market.service.admin.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 管理员模块-角色管理
 *
 * @author 2731235517@qq.com 谢畅
 * @since 2022/12/29 23:00
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {
    @Autowired
    AdminRoleService adminRoleService;

    @RequestMapping("options")
    public BaseRespVo options() {
        CommonData<AdminOptionsVo> commonData = adminRoleService.options();
        return BaseRespVo.ok(commonData);
    }

    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo, String name) {
        CommonData<MarketRole> commonData = adminRoleService.list(basePageInfo, name);
        return BaseRespVo.ok(commonData);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody AdminRoleCreateBo adminRoleCreateBo) {
        AdminRoleCreateVo adminRoleCreateVo;
        try {
            adminRoleCreateVo = adminRoleService.create(adminRoleCreateBo);
        } catch (Exception e) {
            return BaseRespVo.invalidParameter("该角色已经存在");
        }
        return BaseRespVo.ok(adminRoleCreateVo);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketRole marketRole) {
        try {
            adminRoleService.update(marketRole);
        } catch (Exception e) {
            return BaseRespVo.invalidParameter("该角色已经存在");
        }
        return BaseRespVo.ok(marketRole);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketRole marketRole) {
        adminRoleService.delete(marketRole);
        return BaseRespVo.ok();
    }

    @RequestMapping(value = "permissions", method = RequestMethod.GET)
    public BaseRespVo permissionsGet(Integer roleId) {
        AdminPermissionVo adminPermissionVo = adminRoleService.listPermissions(roleId);
        return BaseRespVo.ok(adminPermissionVo);
    }

    @RequestMapping(value = "permissions", method = RequestMethod.POST)
    public BaseRespVo permissionsPost(@RequestBody AdminPermissionPostBo adminPermissionPostBo) {
        if (adminPermissionPostBo.getRoleId() == 1){
            return BaseRespVo.invalidParameter("超级管理员的权限不能修改");
        }
        adminRoleService.updatePermissions(adminPermissionPostBo.getPermissions(), adminPermissionPostBo.getRoleId());
        return BaseRespVo.ok();
    }

    // 通过代码创建权限表， 可以通过公网获取JSON，用postman发送请求
    @RequestMapping("systemPermissions")
    public BaseRespVo systemPermissions(@RequestBody List<AdminPermissionBo> adminPermissionBoList) {
        adminRoleService.insertSystemPermissions(adminPermissionBoList);
        return BaseRespVo.ok();
    }
}
